<?php

namespace App\Admin\Forms\Withdraw;

use App\Models\User;
use App\Models\UserMill;
use App\Models\UserPerformance;
use App\Models\UserTree;
use App\Models\UserWithdraw;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class WithdrawCheckForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $status = $input['status'] ?? "";
        $id = $this->payload['id'];
        $remark = $input['remark'] ?? "";
        /**
         * $@var $withdraw UserWithdraw
         */
        $withdraw = UserWithdraw::query()->where('id', $id)->first();
        if (!$withdraw) {
            return $this->response()->error('订单不存在');
        }

        if ($withdraw->status != TRADE_STATUS_UNTREATED) {
            return $this->response()->error('订单状态有误');
        }


        $res = false;
        switch ($status) {
            case TRADE_STATUS_SUCCESS:
                $res = $withdraw->success();
                break;
            case TRADE_STATUS_FAIL:
                $res = $withdraw->fail();
                break;
        }

        return $res ?
            $this->response()->success('执行成功')->refresh() :
            $this->response()->error('执行失败');
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        /**
         * @var $withdraw UserWithdraw
         */
        $withdraw = UserWithdraw::query()->where('id', $this->payload['id'])->first();
        if (!$withdraw) {

        }
        $this->textarea('info', '信息')
            ->value("
提现地址:$withdraw->to_address
提现金额:$withdraw->real_amount
")
            ->disable();

        $keysToExtract = [TRADE_STATUS_SUCCESS, TRADE_STATUS_FAIL];
        $this->radio('status')->options(array_filter(tt_trade_status('', true), function ($key) use ($keysToExtract) {
            return in_array($key, $keysToExtract);
        }, ARRAY_FILTER_USE_KEY))->default(TRADE_STATUS_SUCCESS);
        $this->text('remark', '备注');
        $this->hidden("id")->value($this->getKey());
    }

    public function default()
    {
        return [
        ];
    }
}
